package com.mq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 延迟消息队列
 * 死信队列
 *
 * @author xwj
 * @date 2019/7/23
 */
@Configuration
public class DelayRabbitConfig {

    /**
     * 正常消息交换机
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("delay_direct", true, false);
    }

    /**
     * 绑定5s失效消息队列
     */
    @Bean
    public Binding directBindingQueue_5s() {
        return BindingBuilder.bind(queue_5s()).to(directExchange()).with("5s");
    }

    /**
     * 绑定10s失效消息队列
     */
    @Bean
    public Binding directBindingQueue_10s() {
        return BindingBuilder.bind(queue_10s()).to(directExchange()).with("10s");
    }

    /**
     * 绑定30s失效消息队列
     */
    @Bean
    public Binding directBindingQueue_30s() {
        return BindingBuilder.bind(queue_30s()).to(directExchange()).with("30s");
    }

    /**
     * 绑定60s失效消息队列
     */
    @Bean
    public Binding directBindingQueue_60s() {
        return BindingBuilder.bind(queue_60s()).to(directExchange()).with("60s");
    }

    /**
     * 5s过期等级队列
     */
    @Bean
    public Queue queue_5s() {
        Map<String, Object> args = new HashMap<>(4);
        // 5秒过期
        args.put("x-message-ttl", 5000);
        // 死信交换机
        args.put("x-dead-letter-exchange", "exchange_dlx_5s");
        // 死信交换机路由键
        args.put("x-dead-letter-routing-key", "delay_5s");
        return new Queue("queue_5s", true, false, false, args);
    }

    /**
     * 5s过期等级延时队列
     */
    @Bean
    public Queue delay_queue_5s() {
        return new Queue("delay_queue_5s", true, false, false);
    }

    /**
     * 5秒死信交换机
     *
     * @return
     */
    @Bean
    public DirectExchange exchange_dlx_5s() {
        return new DirectExchange("exchange_dlx_5s", true, false);
    }

    /**
     * 5秒死信交换机绑定延时队列
     * 实际消费队列
     */
    @Bean
    public Binding bindingQueue_5s() {
        return BindingBuilder.bind(delay_queue_5s()).to(exchange_dlx_5s()).with("delay_5s");
    }

    /**
     * 10s过期等级队列
     */
    @Bean
    public Queue queue_10s() {
        Map<String, Object> args = new HashMap<>(4);
        // 10秒过期
        args.put("x-message-ttl", 10000);
        // 死信交换机
        args.put("x-dead-letter-exchange", "exchange_dlx_10s");
        // 死信交换机路由键
        args.put("x-dead-letter-routing-key", "delay_10s");
        return new Queue("queue_10s", true, false, false, args);
    }

    /**
     * 10s过期等级延时队列
     */
    @Bean
    public Queue delay_queue_10s() {
        return new Queue("delay_queue_10s", true, false, false);
    }

    /**
     * 10秒死信交换机
     *
     * @return
     */
    @Bean
    public DirectExchange exchange_dlx_10s() {
        return new DirectExchange("exchange_dlx_10s", true, false);
    }

    /**
     * 10秒死信交换机绑定延时队列
     */
    @Bean
    public Binding bindingQueue_10s() {
        return BindingBuilder.bind(delay_queue_10s()).to(exchange_dlx_10s()).with("delay_10s");
    }

    /**
     * 30s过期等级队列
     */
    @Bean
    public Queue queue_30s() {
        Map<String, Object> args = new HashMap<>(4);
        // 10秒过期
        args.put("x-message-ttl", 30000);
        // 死信交换机
        args.put("x-dead-letter-exchange", "exchange_dlx_30s");
        // 死信交换机路由键
        args.put("x-dead-letter-routing-key", "delay_30s");
        return new Queue("queue_30s", true, false, false, args);
    }

    /**
     * 30s过期等级延时队列
     */
    @Bean
    public Queue delay_queue_30s() {
        return new Queue("delay_queue_30s", true, false, false);
    }

    /**
     * 30秒死信交换机
     *
     * @return
     */
    @Bean
    public DirectExchange exchange_dlx_30s() {
        return new DirectExchange("exchange_dlx_30s", true, false);
    }

    /**
     * 30秒死信交换机绑定延时队列
     */
    @Bean
    public Binding bindingQueue_30s() {
        return BindingBuilder.bind(delay_queue_30s()).to(exchange_dlx_30s()).with("delay_30s");
    }

    /**
     * 60s过期等级队列
     */
    @Bean
    public Queue queue_60s() {
        Map<String, Object> args = new HashMap<>(4);
        // 10秒过期
        args.put("x-message-ttl", 60000);
        // 死信交换机
        args.put("x-dead-letter-exchange", "exchange_dlx_60s");
        // 死信交换机路由键
        args.put("x-dead-letter-routing-key", "delay_60s");
        return new Queue("queue_60s", true, false, false, args);
    }

    /**
     * 60s过期等级延时队列
     */
    @Bean
    public Queue delay_queue_60s() {
        return new Queue("delay_queue_60s", true, false, false);
    }

    /**
     * 60秒死信交换机
     *
     * @return
     */
    @Bean
    public DirectExchange exchange_dlx_60s() {
        return new DirectExchange("exchange_dlx_60s", true, false);
    }

    /**
     * 60秒死信交换机绑定延时队列
     */
    @Bean
    public Binding bindingQueue_60s() {
        return BindingBuilder.bind(delay_queue_60s()).to(exchange_dlx_60s()).with("delay_60s");
    }

}
